/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ public List<Integer> inorderTraversal(TreeNode root){ List<Integer> res = new ArrayList<>(); Deque<TreeNode> stack = new ArrayDeque<>(); TreeNode p = root; while (p!=null|| !stack.isEmpty()) { while (p!=null) { stack.push(p); p=p.left; }
if (!stack.isEmpty()) { p = stack.pop(); res.add(p.val); p=p.right; } } return res; } }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ //这个pre非常巧妙 long pre = Long.MIN_VALUE; publicbooleanisValidBST(TreeNode root){ if (root==null) { returntrue; } if(!isValidBST(root.left)){ returnfalse; };
if (root.val <= pre) { returnfalse; } pre = root.val; return isValidBST(root.right); } }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root==null) { return res; } LinkedList<TreeNode> queue = new LinkedList<>(); queue.add(root); int nextLevel = 0; int toBePrinted = 1; ArrayList<Integer> temp = new ArrayList<>();
while (!queue.isEmpty()) { TreeNode p =queue.pop(); temp.add(p.val); toBePrinted--;
if (p.left!=null) { queue.add(p.left); nextLevel++; }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ public List<List<Integer>> zigzagLevelOrder(TreeNode root) { if (root == null) { returnnew ArrayList<List<Integer>>(); }
List<List<Integer>> results = new ArrayList<List<Integer>>();
// add the root element with a delimiter to kick off the BFS loop LinkedList<TreeNode> node_queue = new LinkedList<TreeNode>(); node_queue.addLast(root); //把null作为结束 node_queue.addLast(null);
LinkedList<Integer> level_list = new LinkedList<Integer>(); boolean is_order_left = true;
while (node_queue.size() > 0) { TreeNode curr_node = node_queue.pollFirst(); if (curr_node != null) { if (is_order_left) level_list.addLast(curr_node.val); else level_list.addFirst(curr_node.val);
if (curr_node.left != null) node_queue.addLast(curr_node.left); if (curr_node.right != null) node_queue.addLast(curr_node.right);
} else { // we finish the scan of one level results.add(level_list); level_list = new LinkedList<Integer>(); // prepare for the next level if (node_queue.size() > 0) node_queue.addLast(null); is_order_left = !is_order_left; } } return results; } }
//发现其实这些 end没啥大用,就是为了好理解 public TreeNode BuildTree(int pStart,int pEnd, int inStart,int inEnd){ //第一个是根节点 TreeNode root = new TreeNode(preorder[pStart]); if (pStart==pEnd) { return root; } int i=inStart; //从中序遍历中找出根节点,将其分开 for (; i <=inEnd; i++) { if (preorder[pStart] == inorder[i]) { break; } } int len = i-inStart; if (pStart+1<=pStart+len) root.left = BuildTree(pStart+1,pStart+len,inStart,i-1); if (pStart+len+1<=pEnd) root.right = BuildTree(pStart+len+1,pEnd,i+1,inEnd); return root; } }
public TreeNode BuildTree(int pStart,int pEnd, int inStart,int inEnd){
//最后一个是根节点 TreeNode root = new TreeNode(postorder[pEnd]); if (pStart==pEnd) { return root; } int i=inStart; //从中序遍历中找出根节点,将其分开 for (; i <=inEnd; i++) { if (postorder[pEnd] == inorder[i]) { break; } } int len = i-inStart; if (pStart<=pStart+len-1) root.left = BuildTree(pStart,pStart+len-1,inStart,i-1); if (pStart+len<=pEnd-1) root.right = BuildTree(pStart+len,pEnd-1,i+1,inEnd); return root; } }