Hi everybody, I'm
方圆
table of Contents
Tree traversal
94. In-order traversal of binary tree
//递归
class Solution {
private List<Integer> res = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
dfs(root);
return res;
}
private void dfs(TreeNode root){
if(root == null) return;
dfs(root.left);
res.add(root.val);
dfs(root.right);
}
}
//迭代
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty() || root != null){
while(root != null){
stack.push(root);
root = root.left;
}
root = stack.pop();
res.add(root.val);
root = root.right;
}
return res;
}
}
102. Sequence traversal of binary tree
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(queue.size() > 0){
int size = queue.size();
ArrayList<Integer> temp = new ArrayList<>();
for(int i = 0;i < size;i++){
TreeNode node = queue.poll();
temp.add(node.val);
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
}
res.add(temp);
}
return res;
}
}
110. Balanced Binary Tree
class Solution {
public boolean isBalanced(TreeNode root) {
return recur(root) != -1;
}
private int recur(TreeNode root){
if(root == null) return 0;
int left = recur(root.left);
if(left == -1) return -1;
int right = recur(root.right);
if(right == -1) return -1;
return Math.abs(left - right) < 2 ? Math.max(left,right) + 1 : -1;
}
}
144. Preorder traversal of binary tree
//递归
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
dfs(root);
return res;
}
private void dfs(TreeNode root){
if(root == null) return;
res.add(root.val);
dfs(root.left);
dfs(root.right);
}
}
//迭代
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root == null) return res;
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty() || root != null){
if(root != null){
res.add(root.val);
stack.push(root);
root = root.left;
}else{
TreeNode temp = stack.pop();
root = temp.right;
}
}
return res;
}
}
145. Post-order traversal of binary tree
//递归
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
dfs(root);
return res;
}
private void dfs(TreeNode root){
if(root == null) return;
dfs(root.left);
dfs(root.right);
res.add(root.val);
}
}
//迭代
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> res = new LinkedList<>();
if(root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
while(!stack.isEmpty()){
TreeNode temp = stack.pop();
res.addFirst(temp.val);
if(temp.left != null) stack.push(temp.left);
if(temp.right != null) stack.push(temp.right);
}
return res;
}
}
Binary search tree related
98. Verify Binary Search Tree
class Solution {
long pre = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if(root == null) return true;
boolean left = isValidBST(root.left);
if(root.val <= pre) return false;
pre = root.val;
boolean right = isValidBST(root.right);
return left && right;
}
}
450. Delete a node in the binary search tree
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return root;
if(root.val > key){
root.left = deleteNode(root.left,key);
}else if(root.val < key){
root.right = deleteNode(root.right,key);
}else{
if(root.left != null && root.right != null){
TreeNode temp = root.right;
while(temp.left != null)
temp = temp.left;
temp.left = root.left;
return root.right;
}
if(root.left == null) return root.right;
if(root.right == null) return root.left;
}
return root;
}
}
701. Insertion in Binary Search Tree
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
//找到空节点要么插左边要么插右边
if(root == null) return new TreeNode(val);
if(root.val < val)
root.right = insertIntoBST(root.right,val);
else if(root.val > val)
root.left = insertIntoBST(root.left,val);
return root;
}
}