🟢 剑指 Offer II 052. 展平二叉搜索树
LeetCode 提示
题目难度 简单
原题链接 🔗 leetcode
#
题解1/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution { private TreeNode theRoot; private TreeNode cur;
private void join(TreeNode node) { if (this.cur == null) { this.theRoot = node; this.cur = this.theRoot; } else { this.cur.right = node; this.cur = this.cur.right; } }
private void visit(TreeNode node) { if (node.left != null) { TreeNode left = node.left; node.left = null; visit(left); } join(node); if (node.right != null) { TreeNode right = node.right; node.right = null; visit(right); } }
public TreeNode increasingBST(TreeNode root) { this.theRoot = null; this.cur = null;
visit(root); return this.theRoot; }}