【问题描述】[中等]
【解答思路】
中序遍历
时间复杂度:O(N) 空间复杂度:O(N)
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);}
}
【总结】
1.二叉搜索树中序遍历
// 打印中序遍历
void dfs(TreeNode root) {if(root == null) return;dfs(root.left); // 左System.out.println(root.val); // 根dfs(root.right); // 右
}
2.二叉搜索树中序遍历 单调地震
3.树 递归 画图
作者:Krahets
转载链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/solution/mian-shi-ti-36-er-cha-sou-suo-shu-yu-shuang-xian-5/