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
| public class Solution { | |
| private TreeNode firstNode = null; | |
| private TreeNode secondNode = null; | |
| private TreeNode preNode = new TreeNode(Integer.MIN_VALUE); | |
| public void recoverTree(TreeNode root) { | |
| traverse(root); | |
| Integer tmp = firstNode.val; | |
| firstNode.val = secondNode.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
| public class Solution { | |
| public List<String> binaryTreePaths(TreeNode root) { | |
| List<String> res = new ArrayList<String>(); | |
| if (root != null) dfs(root, "", res); | |
| return res; | |
| } | |
| private void dfs(TreeNode root, String path, List<String> res) { | |
| if (root.left == null && root.right == null) res.add(path + root.val); | |
| if (root.left != null) dfs(root.left, path+root.val+"->", res); | |
| if (root.right != null) dfs(root.right, path+root.val+"->", res); |
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; |
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 { | |
| 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> 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 { | |
| 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> 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 { | |
| // 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 { | |
| // 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<>(); | |