This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| // O(n) O(n) | |
| private int count = 0; | |
| public int countUnivalSubtrees(TreeNode root) { | |
| unival(root, 0); | |
| return count; | |
| } | |
| private boolean unival(TreeNode node, int val) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| // O(n) O(n/h) | |
| public int minDepth(TreeNode root) { | |
| if (root == null) return 0; | |
| int left = minDepth(root.left); | |
| int right = minDepth(root.right); | |
| return (left == 0 || right == 0) ? left + right + 1: Math.min(left, right) + 1; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| // O(n^2) O(n^2) | |
| public List<TreeNode> generateTrees(int n) { | |
| if (n == 0) return new ArrayList<TreeNode>(); | |
| return genTree(1, n); | |
| } | |
| private List<TreeNode> genTree(int start, int end) { | |
| List<TreeNode> list = new ArrayList<>(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| // O(n^2) O(n) | |
| public int rob(TreeNode root) { | |
| return dfs(root, false); | |
| } | |
| public int dfs(TreeNode node, Boolean robbed) { | |
| if ( node == null ) return 0; | |
| int rob_node = 0, not_rob_node = 0; | |
| if (robbed) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public List<Integer> postorderTraversal(TreeNode root) { | |
| LinkedList<Integer> result = new LinkedList<Integer>(); | |
| TreeNode curNode = root; | |
| Deque<TreeNode> stack = new ArrayDeque<TreeNode>(); | |
| while ( !stack.isEmpty() || curNode != null ) { | |
| if (curNode != null) { | |
| stack.push(curNode); | |
| result.addFirst(curNode.val); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public List<Integer> preorderTraversal(TreeNode root) { | |
| List<Integer> result = new ArrayList<Integer>(); | |
| Stack<TreeNode> stack = new Stack<TreeNode>(); | |
| TreeNode curNode = root; | |
| while ( !stack.isEmpty() || curNode != null) { | |
| if (curNode != null) { | |
| result.add(curNode.val); | |
| stack.push(curNode); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public List<Integer> inorderTraversal(TreeNode root) { | |
| List<Integer> result = new ArrayList<Integer>(); | |
| Stack<TreeNode> stack = new Stack<TreeNode>(); | |
| while ( !stack.isEmpty() || root != null ) { | |
| if ( root != null ) { | |
| stack.push(root); | |
| root = root.left; | |
| } else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| private Integer result; | |
| private Integer count = 0; | |
| public int kthSmallest(TreeNode root, int k) { | |
| dfs(root, k); | |
| return result; | |
| } | |
| public void dfs(TreeNode node, int k) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public List<Integer> rightSideView(TreeNode root) { | |
| List<Integer> res = new ArrayList<Integer>(); | |
| if (root == null) return res; | |
| Deque<TreeNode> dq = new LinkedList<TreeNode>(); | |
| dq.addLast(root); | |
| while (!dq.isEmpty()) { | |
| res.add(dq.peekLast().val); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public void flatten(TreeNode root) { | |
| while (root != null) { | |
| TreeNode rightNode = root.right; | |
| root.right = root.left; | |
| root.left = null; | |
| // find rightmost node | |
| TreeNode node = root; | |
| while (node.right != null) { | |
| node = node.right; |