LeetCode brush title notes (tree)

1.  minimum-depth-of-binary-tree

Title Description

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Seeking the minimum depth of the binary tree;
 

Problem-solving ideas

Recursive traversal of each node binary tree:

(1) If the node is null, 0 is returned depth;

(2) If the node is not empty, return the left and right in a small child that the child's depth +1;

Note: (2) the problem is likely to arise, if only one node left child, no right child, then return at this time should be left child of depth, and should not be considered simply returns the minimum value of the two;

1 public class Solution {
2     public int run(TreeNode root) {
3         if(root==null)
4             return 0;
5         int right = run(root.right);
6         int left = run(root.left);
7         return (left==0 || right==0)? (left+right+1) : Math.min(left,right)+1; 
8     }
9 }

 

2. binary-tree-postorder-traversal

Title Description

A binary Tree GIVEN, The return  postorder  Traversal of ITS Nodes' values. After traversing Binary Tree

For example:
Given binary tree{1,#,2,3},

   1
    \
     2
    /
   3

return[3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

Problem-solving ideas:

Traversal sequence after sequence is: Left -> Right -> root, recursive methods well write:

 1 public class Solution {
 2     ArrayList<Integer> list = new ArrayList<Integer>();
 3     public ArrayList<Integer> postorderTraversal(TreeNode root) {
 4         if(root==null)
 5             return list;
 6         if(root.left!=null)
 7             postorderTraversal(root.left);
 8         if(root.right!=null)
 9             postorderTraversal(root.right);
10         list.add(root.val);
11         return list;
12     }
13 }

Non-recursive method, using the stack to the iteration:

要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点P,先将其入栈。
(1)如果P不存在左孩子和右孩子,则可以直接访问它;
(2)或者P存在孩子,但是其孩子都已被访问过了,则同样可以直接访问该结点
(3)若非上述两种情况,则将P的右孩子和左孩子依次入栈,这样就保证了
注意:每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。
 1 public class Solution {
 2     public ArrayList<Integer> postorderTraversal(TreeNode root) {
 3         ArrayList<Integer> list = new ArrayList<Integer>();
 4         if(root==null)
 5             return list;
 6         Stack<TreeNode> S = new Stack<TreeNode>();
 7         TreeNode preNode = null;
 8         S.push(root);
 9         while(!S.isEmpty()){
10             TreeNode curNode = S.peek();
11             if((curNode.left==null && curNode.right==null)||
12                (preNode != null && (preNode == curNode.left || preNode == curNode.right))){
13                 list.add(curNode.val);
14                 S.pop();
15                 preNode = curNode;
16             }
17             else{
18                 if(curNode.right!=null)
19                     S.push(curNode.right);
20                 if(curNode.left!=null)
21                     S.push(curNode.left);
22             }
23         }
24         return list;
25     }
26 }

 

3. binary-tree-preorder-traversal

Title Description

A binary Tree GIVEN, The return  preorder  Traversal of ITS Nodes' values. Nonrecursive before traversing Binary Tree

For example:
Given binary tree{1,#,2,3},

   1
    \
     2
    /
   3 

return[1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

Problem-solving ideas:

Using the stack structure, sequence preorder is about root, root advanced stack, each time a node is popped from the stack access, and it's children (not empty) stack according to the first left after the right order, until the stack Is empty;
 1 public class Solution {
 2     public ArrayList<Integer> preorderTraversal(TreeNode root) {
 3         Stack<TreeNode> S = new Stack<TreeNode>();
 4         ArrayList<Integer> list = new ArrayList<Integer>();
 5         if(root==null){
 6             return list;
 7         }
 8         S.push(root);
 9         while(!S.isEmpty()){
10             TreeNode tmp = S.pop();
11             list.add(tmp.val);
12             if(tmp.right!=null)
13                 S.push(tmp.right);
14             if(tmp.left!=null)
15                 S.push(tmp.left);
16         }
17         return list;
18     }
19 }

 

4. sum-root-to-leaf-numbers

Title Description

Given a binary tree containing digits from 0-9 only, 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   3

The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.

Return the sum = 12 + 13 =25.

Each path from the root node to the leaf node of the binary tree has may be represented by a number, and seeking these figures;

Problem-solving ideas:

Use recursion to do, mainly considering the recursive condition and end condition. Recursive conditions, i.e. the sum is multiplied by the current 10 plus the current node and passed to the next function recursively, a final sum of the left and right subtrees are added. If the end condition is that if a node is a leaf, then we should accumulate the sum of the results, if the node to the empty node, not a leaf node, without adding to the result, directly back to 0. It is essentially a preorder .

 1 public class Solution {
 2     public int sumNumbers(TreeNode root) {
 3         return Count(root, 0 );
 4     }
 5     static int Count(TreeNode root,int sum){
 6         if(root==null)
 7             return 0;
 8         if(root.left==null&&root.right==null)
 9             return sum*10+root.val;
10         return Count(root.left,sum*10+root.val)+Count(root.right,sum*10+root.val);
11     }
12 }

5. binary-tree-maximum-path-sum

Title Description

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

Return 6.

 Find the binary tree and the largest one path (any node as a starting point, not the root, do not repeat, do not intersect)

Problem-solving ideas:

Starting from the root node, recursively find the left and right sub-tree of each node and the maximum path, the current node returns the value + left subtree and the larger;

Note that when traversing each possible path, and is less than the maximum will be updated currently this path and is then updated;

 1 public class Solution {
 2     public int sum = Integer.MIN_VALUE;
 3     public int maxPathSum(TreeNode root) {
 4         if(root==null)
 5             return 0;
 6         max(root);
 7         return sum;
 8     }
 9     public int max(TreeNode root){
10         if(root==null)
11             return 0;
12         int left = Math.max(0, max(root.left));
13         int right = Math.max(0, max(root.right));
14         sum = Math.max(sum, left+right+root.val);
15         return Math.max(left, right)+root.val;
16     }
17 }

 

 

Guess you like

Origin www.cnblogs.com/PJQOOO/p/10978662.html