S算法问题: Binary Tree Maximum Path Sum
问题描述:
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.
S算法问题: Binary Tree Postorder Traversal
问题描述:
Given a binary tree, return the postorder traversal of its nodes' values.
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?
算法问题推荐解法来了...
1. Iterative way (stack). Time: O(n), Space: O(n).
2. Recursive solution. Time: O(n), Space: O(n).
3. Threaded tree (Morris). Time: O(n), Space: O(1).
You may refer to my blog for more detailed explanations:
www.cnblogs.com/AnnieK...ersal.html
解法(Java)
代码: |
import java.util.ArrayList;
import java.util.Stack;
public class BinaryTreePostorderTraversal {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> ret = new ArrayList<Integer>();
if (root == null)
return ret;
Stack<TreeNode> st = new Stack<TreeNode>();
TreeNode p = root.right;
ret.add(root.val);
st.add(root);
while (!st.isEmpty()) {
while (p != null) {
ret.add(p.val);
st.add(p);
p = p.right;
}
TreeNode node = st.pop();
p = node.left;
if (p != null) {
ret.add(p.val);
st.add(p);
p = p.right;
}
}
int i = 0;
int j = ret.size() - 1;
while(i < j) {
int tmp = ret.get(i);
ret.set(i, ret.get(j));
ret.set(j, tmp);
i++;
j--;
}
return ret;
}
}
|
S算法问题: Binary Tree Preorder Traversal
问题描述:
Given a binary tree, return the preorder traversal of its nodes' values.
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?
S算法问题: Binary Tree Zigzag Level Order Traversal
问题描述:
Given a binary tree, return the zigzag level order traversal 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 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
算法问题推荐解法来了...
1. Queue + reverse.
2. Two stacks.
3. Vector. Contributed by yinlinglin.
oops, 这题还没有解法,你来试一试?
S算法问题: Candy
问题描述:
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
- Each child must have at least one candy.
- Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
算法问题推荐解法来了...
You may refer to
github.com/AnnieKim/IT...%9E%9C.cpp
1. traverse only once with O(1) space. 2. O(n) space.
解法(Java)
代码: |
public class Candy {
public int candy(int[] ratings) {
int[] candy = new int[ratings.length];
candy[0] = 1;
for (int i = 1; i < ratings.length; i++) {
candy[i] = ratings[i] > ratings[i - 1] ? candy[i - 1] + 1 : 1;
}
int totalCandy = candy[ratings.length - 1];
for (int i = ratings.length - 2; i >= 0; i--) {
candy[i] = (ratings[i] > ratings[i + 1] && candy[i + 1] + 1 > candy[i]) ? candy[i + 1] + 1 : candy[i];
totalCandy += candy[i];
}
return totalCandy;
}
}
|
S算法问题: Climbing Stairs
问题描述:
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?