897. Increasing Order Search Tree

中序遍历升级版,向右压扁一个bst,写不出的。思路是左边的中序遍历加上root,加上右边的中序遍历。

就像这样:res = root.left ---> root ---> root.right,然后再看看递归函数,是不是写的很有道理

public TreeNode increasingBST(TreeNode root) {
    return dfs(root, null);
}

public TreeNode dfs(TreeNode root, TreeNode tail) {
    if (root == null) return tail;
    TreeNode res = dfs(root.left, root);
    root.left = null;
    root.right = dfs(root.right, tail);
    return res;
}

results matching ""

    No results matching ""