|
| 1 | +package tencent_cloud; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class Solution2 { |
| 7 | + |
| 8 | +private static TreeNode root; |
| 9 | + |
| 10 | +static class TreeNode { |
| 11 | +int val; |
| 12 | +TreeNode left; |
| 13 | +TreeNode right; |
| 14 | + |
| 15 | +public TreeNode() { |
| 16 | +this.val = val; |
| 17 | +this.left = null; |
| 18 | +this.right = null; |
| 19 | +} |
| 20 | + |
| 21 | +public TreeNode(int val) { |
| 22 | +this.val = val; |
| 23 | +this.left = null; |
| 24 | +this.right = null; |
| 25 | +} |
| 26 | +} |
| 27 | + |
| 28 | +public static boolean isValidBST(TreeNode root) { |
| 29 | +long pre = Long.MIN_VALUE; |
| 30 | +Stack<TreeNode> stack = new Stack<>(); |
| 31 | +while (root != null) { |
| 32 | +stack.push(root); |
| 33 | +root = root.left; |
| 34 | +} |
| 35 | +while (!stack.empty()) { |
| 36 | +TreeNode temp = stack.pop(); |
| 37 | +if (temp.val <= pre) |
| 38 | +return false; |
| 39 | +pre = temp.val; |
| 40 | +if (temp.right != null) { |
| 41 | +TreeNode r = temp.right; |
| 42 | +while (r != null) { |
| 43 | +stack.push(r); |
| 44 | +r = r.left; |
| 45 | +} |
| 46 | +} |
| 47 | +} |
| 48 | +return true; |
| 49 | +} |
| 50 | + |
| 51 | +public static void addTreeNode(TreeNode node, String[] nodes) { |
| 52 | +// if (root == null) root = new TreeNode(data); |
| 53 | +// |
| 54 | +// //采用随机方式创建二叉树 |
| 55 | +// if (Math.random() > 0.5) { |
| 56 | +// if (node.left == null) { |
| 57 | +// node.left = new TreeNode(data); |
| 58 | +// } else { |
| 59 | +// addTreeNode(node.left, data); |
| 60 | +// } |
| 61 | +// } else { |
| 62 | +// if (node.right == null) { |
| 63 | +// node.right = new TreeNode(data); |
| 64 | +// } else { |
| 65 | +// addTreeNode(node.right, data); |
| 66 | +// } |
| 67 | +// } |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +public static void creat(int[] nodes) { |
| 72 | +List<TreeNode> datas = new ArrayList<>(); |
| 73 | +// 将一个数组的值依次转换为Node节点 |
| 74 | +for (int val : nodes) { |
| 75 | +datas.add(new TreeNode(val)); |
| 76 | +} |
| 77 | +// 第一个数为根节点 |
| 78 | +root = datas.get(0); |
| 79 | +// 建立二叉树 |
| 80 | +for (int i = 0; i < nodes.length / 2; i++) { |
| 81 | +// 左孩子 |
| 82 | +datas.get(i).left = datas.get(i * 2 + 1); |
| 83 | +// 右孩子 |
| 84 | +if (i * 2 + 2 < datas.size()) {//避免偶数的时候 下标越界 |
| 85 | +datas.get(i).right = datas.get(i * 2 + 2); |
| 86 | +} |
| 87 | +} |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +public static void levelOrder(TreeNode root) { |
| 92 | +if (root == null) return; |
| 93 | + |
| 94 | +Queue<TreeNode> queue = new LinkedList<>(); |
| 95 | +queue.add(root); |
| 96 | +while (!queue.isEmpty()) { |
| 97 | +TreeNode node = queue.poll(); |
| 98 | +System.out.print(node.val + " "); |
| 99 | +if (node.left != null) queue.add(node.left); |
| 100 | +if (node.right != null) queue.add(node.right); |
| 101 | +} |
| 102 | +} |
| 103 | + |
| 104 | +public static void main(String[] args) { |
| 105 | +int[] nodes = {1, 8, 9, 11, 10, 6, 7, 3}; |
| 106 | +creat(nodes); |
| 107 | +levelOrder(root); |
| 108 | +} |
| 109 | +} |
0 commit comments