1.1、题目1

剑指 Offer 54. 二叉搜索树的第k大节点

1.2、解法

后面再回来看这里。

1.3、代码

class Solution {
    int res,k;
    public int kthLargest(TreeNode root, int k) {
        this.k=k;
        dfs(root);
        return res;
    }   

    public void dfs(TreeNode root){
        if(root==null) return ;
        dfs(root.right);
        if(k==0) return;
        if(--k==0) res=root.val;
        dfs(root.left);
    }
}

2.1、题目2

剑指 Offer 36. 二叉搜索树与双向链表

2.2、解法

后面再回来看这里。

2.3、代码

class Solution {
    Node pre, head;
    public Node treeToDoublyList(Node root) {
        if(root == null) return null;
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }
    void dfs(Node cur) {
        if(cur == null) return;
        dfs(cur.left);
        if(pre != null) pre.right = cur;
        else head = cur;
        cur.left = pre;
        pre = cur;
        dfs(cur.right);
    }
}


3.1、题目3

剑指 Offer 34. 二叉树中和为某一值的路径

3.2、解法

后面再回来看这里。

3.3、代码

class Solution {
    LinkedList<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>(); 
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        recur(root, sum);
        return res;
    }
    void recur(TreeNode root, int tar) {
        if(root == null) return;
        path.add(root.val);
        tar -= root.val;
        if(tar == 0 && root.left == null && root.right == null)
            res.add(new LinkedList(path));
        recur(root.left, tar);
        recur(root.right, tar);
        path.removeLast();
    }
}