随笔分类 - leetcode-tree
摘要:1 public static TreeNode DeSerilize(ArrayListres,int[] i){ 2 if(res.get(i[0]).equals("#")||i[0]==res.size()) return null; 3 TreeNode ...
阅读全文
posted @ 2014-03-22 06:32
krunning
摘要:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.F...
阅读全文
posted @ 2014-02-24 04:56
krunning
摘要:Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],] 1 p...
阅读全文
posted @ 2014-02-22 13:11
krunning
摘要:1 public class Solution { 2 public ArrayList preorderTraversal(TreeNode root) { 3 ArrayList res = new ArrayList(); 4 if(root==null) return res; 5 TreeNode cur = root; 6 Stackstack = new Stack(); 7 while(!stack.isEmpty() || cur!=null){ 8 if(cur!...
阅读全文
posted @ 2014-02-19 05:31
krunning
摘要:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 1 public class Solution { 2 ...
阅读全文
posted @ 2014-02-19 05:29
krunning
摘要:1 public class Solution { 2 public ArrayList postorderTraversal(TreeNode root) { 3 ArrayList res = new ArrayList(); 4 if(root==null) return res; 5 TreeNode cur = null, pre = null; 6 Stackstack = new Stack(); 7 stack.push(root); 8 while(!stack.isEm...
阅读全文
posted @ 2014-02-19 05:25
krunning
摘要:Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ...
阅读全文
posted @ 2014-02-19 00:38
krunning
摘要:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ...
阅读全文
posted @ 2014-02-19 00:27
krunning
摘要:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]...
阅读全文
posted @ 2014-02-17 02:15
krunning
摘要:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 7After calling your function, the tree s...
阅读全文
posted @ 2014-02-17 02:07
krunning
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3 Return6. 1 public class Solution { 2 public int maxPathSum(TreeNode root) { 3 int res []={Integer.MIN_VALUE}; 4 he...
阅读全文
posted @ 2014-02-17 01:46
krunning
摘要:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure. 1 public class Solution { 2 public void recoverTree(TreeNode root) { 3 if(root==null) return ; 4 ArrayList t = new ArrayList (); 5 ArrayList v = new ArrayLis...
阅读全文
posted @ 2014-02-17 01:40
krunning
摘要:1 public class Solution { 2 public TreeNode buildTree(int[] inorder, int[] postorder) { 3 int inEnd = inorder.length-1; 4 int postEnd = postorder.length-1; 5 if(inEnd0)19 root.left = build(in,inStart,piv-1,post,postStart,postStart+leftLen-1);20 if(piv...
阅读全文
posted @ 2014-02-12 04:08
krunning
摘要:1 public class Solution { 2 public TreeNode buildTree(int[] preorder, int[] inorder) { 3 int len =preorder.length; 4 if(lenpreE || inS>inE) return null; 9 int first = pre[preS];10 TreeNode p = new TreeNode(first);11 if(preS==preE) // don't forget this12 ...
阅读全文
posted @ 2014-02-12 04:01
krunning
摘要:Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3], ...
阅读全文
posted @ 2014-02-06 15:31
krunning
摘要:1 public class Solution { 2 public boolean isSameTree(TreeNode p, TreeNode q) { 3 if(p==null) return q==null; 4 if(q==null) return p==null; 5 if(p.val==q.val){ 6 return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right); 7 } 8 else return ...
阅读全文
posted @ 2014-02-06 15:22
krunning
摘要:1 public class Solution { 2 public boolean isBalanced(TreeNode root) { 3 if(root==null) return true; 4 if(checkHeight(root)==-1) return false; 5 return true; 6 } 7 public int checkHeight(TreeNode root){ 8 if(root==null) return 0; 9 int left = chec...
阅读全文
posted @ 2014-02-06 15:20
krunning
摘要:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.T
阅读全文
posted @ 2014-02-06 15:02
krunning
摘要:1 public class Solution { 2 public ArrayList inorderTraversal(TreeNode root) { 3 ArrayList res = new ArrayList (); 4 Stack stack = new Stack(); 5 TreeNode cur = root; 6 while(!stack.isEmpty()||cur!=null){ 7 if(cur!=null){ 8 stack.push(cur); 9 ...
阅读全文
posted @ 2014-02-06 14:59
krunning
摘要:1 public class Solution { 2 public boolean isValidBST(TreeNode root) { 3 return is(root,Integer.MAX_VALUE,Integer.MIN_VALUE); 4 } 5 public boolean is(TreeNode root,int max,int min){ 6 if(root==null) 7 return true; 8 if(root.valmin){ 9 retur...
阅读全文
posted @ 2014-02-06 14:58
krunning
浙公网安备 33010602011771号