LC144. Binary Tree Preorder Traversal
Input: [1,null,2,3]
1
2
/
3
Output: [1,2,3]
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
TreeNode node = stack.pop();
list.add(node.val);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
return list;
}
LC94. Binary Tree Inorder Traversal
Input: [1,null,2,3]
1
2
/
3
Output: [1,3,2]
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()){
while (curr != null){
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
list.add(curr.val);
curr = curr.right;
}
return list;
}
LC107. Binary Tree Level Order Traversal II
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (root == null) return list;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root); // instead of using q.add(belongs to collection)
while(!q.isEmpty()){
List<Integer> list1 = new ArrayList<Integer>();
int size = q.size();
for (int i = 0; i < size; i++){
if (q.peek().left != null)
q.offer(q.peek().left);
if (q.peek().right != null)
q.offer(q.peek().right);
list1.add(q.poll().val);// instead of remove to get element and remove.
}
list.add(0,list1); //move list1 to the
}
return list;
}
LC98. Validate Binary Search Tree
5
/
1 4
/
3 6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
is 5 but its right child's value is 4.
class ResultType {
int maxValue, minValue;
boolean isBST;
public ResultType(boolean isBST, int minValue, int maxValue) {
this.maxValue = maxValue;
this.minValue = minValue;
this.isBST = isBST;
}
}
public class Solution {
public boolean isValidBST(TreeNode root) {
return helper(root).isBST;
}
private ResultType helper(TreeNode root){
if (root == null) return new ResultType(true, Integer.MAX_VALUE, Integer.MIN_VALUE);
ResultType left = helper(root.left);
ResultType right = helper(root.right);
if (!left.isBST || !right.isBST)
return new ResultType(false, 0, 0);
if (root.left!= null && left.maxValue >= root.val || root.right != null && right.minValue <= root.val)
return new ResultType(false, 0, 0);
return new ResultType(true, Math.min(root.val, left.minValue), Math.max(root.val, right.maxValue));
}
}
LC104. Maximum Depth of Binary Tree
Given binary tree [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
return its depth = 3.
public int maxDepth(TreeNode root) {
if (root == null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
LC110. Balanced Binary Tree
class ResultType {
int maxDepth;
boolean isBalanced;
public ResultType (int maxDepth, boolean isBalanced){
this.maxDepth = maxDepth;
this.isBalanced = isBalanced; //返回深度和是否是平衡
}
}
public class Solution {
public boolean isBalanced(TreeNode root) {
return helper(root).isBalanced;
}
public ResultType helper(TreeNode root){
if (root == null) return new ResultType(0, true); // 新类需要加new;
ResultType left = helper(root.left);
ResultType right = helper(root.right);
if (left.isBalanced == false || right.isBalanced == false || Math.abs(left.maxDepth-right.maxDepth) > 1){
return new ResultType(-1, false);
}
return new ResultType(Math.max(left.maxDepth, right.maxDepth) + 1, true);
}
}