Anne

Anne.github.io


  • Home

  • About

  • Tags

  • Categories

  • Archives

116.Populating Next Right Pointers in Each Node

Posted on 2018-06-21 | In LeetCode

Tree, Medium

Question

Given a binary tree

1
2
3
4
5
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 to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • Recursive approach is fine, implicit stack space does not count as extra space for this problem.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

Example:

Given the following perfect binary tree,

1
2
3
4
5
1
/ \
2 3
/ \ / \
4 5 6 7

After calling your function, the tree should look like:

1
2
3
4
5
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
Read more »

513.Find Bottom Left Tree Value

Posted on 2018-06-20 | In LeetCode

Tree, Medium

Question

Given a binary tree, find the leftmost value in the last row of the tree.

Example 1:

1
2
3
4
5
6
7
8
Input:
2
/ \
1 3
Output:
1

Example 2:

1
2
3
4
5
6
7
8
9
10
11
12
Input:
1
/ \
2 3
/ / \
4 5 6
/
7
Output:
7

Note: You may assume the tree (i.e., the given root node) is not NULL.

Read more »

199.Binary Tree Right Side View

Posted on 2018-06-20 | In LeetCode

Tree, Medium

Question

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

1
2
3
4
5
6
7
8
9
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---
Read more »

107.Binary Tree Level Order Traversal II

Posted on 2018-06-20 | In LeetCode

Question

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

1
2
3
4
5
3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

1
2
3
4
5
[
[15,7],
[9,20],
[3]
]
Read more »

102.Binary Tree Level Order Traversal

Posted on 2018-06-20 | In LeetCode

Tree, Medium

Question

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

1
2
3
4
5
3
/ \
9 20
/ \
15 7

return its level order traversal as:

1
2
3
4
5
[
[3],
[9,20],
[15,7]
]
Read more »

LeetCode Tree Traversal Summary

Posted on 2018-06-20 | In LeetCode
Here I summarize the iterative implementation for preorder, inorder, and postorder traverse. PreOrder model12345678public static void helper(List<Integer> res, TreeNode root){ if(root == null) return ; // 操作 // 终止条件 helper(res, root.left); helper(res, root.right);} Inorder model1234567public static void helper(List<Integer> res, TreeNode root){ if(root == null) return null; helper(res, root.left); // 操作 helper(res, root.right);} 144.Pre Order TraverseRecursive1234567891011121314151617181920212223/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { ...
Read more »

145.Binary Tree Postorder Traversal

Posted on 2018-06-19 | In LeetCode

Question

Given a binary tree, return the postorder traversal of its nodes’ values.

Example:

1
2
3
4
5
6
7
8
Input: [1,null,2,3]
1
\
2
/
3
Output: [3,2,1]

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

Read more »

94.Binary Tree Inorder Traversal

Posted on 2018-06-19 | In LeetCode

Question

Given a binary tree, return the inorder traversal of its nodes’ values.

Example:

1
2
3
4
5
6
7
8
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]

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

Read more »

144.Binary Tree Preorder Traversal

Posted on 2018-06-19 | In LeetCode

Question

Given a binary tree, return the preorder traversal of its nodes’ values.

Example:

1
2
3
4
5
6
7
8
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,2,3]

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

Read more »

84.Largest Rectangle in Histogram

Posted on 2018-06-19 | In LeetCode

Question

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

img
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

img
The largest rectangle is shown in the shaded area, which has area = 10 unit.

Example:

1
2
Input: [2,1,5,6,2,3]
Output: 10
Read more »
1…678…14
Anne_ZAJ

Anne_ZAJ

boom pow

134 posts
14 categories
17 tags
0%
© 2019 Anne_ZAJ
Powered by Hexo
|
Theme — NexT.Pisces v5.1.3