zoukankan      html  css  js  c++  java
  • [LeetCode] 897. Increasing Order Search Tree

    Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child. 

    Example 1:

    Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
    Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
    

    Example 2:

    Input: root = [5,1,7]
    Output: [1,null,5,null,7]

    Constraints:

    • The number of nodes in the given tree will be in the range [1, 100].
    • 0 <= Node.val <= 1000

    递增顺序搜索树。

    给你一个树,请你 按中序遍历 重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有一个右子结点。

    这道题跟之前的114题很像,也是属于需要把树做扁平化处理的题目。既然题目都说了用中序遍历,我这里给出两种做法,迭代和递归。时间空间复杂度均是 O(n)。代码应该是 self-explained,只是需要注意当处理节点的时候,需要找到新的 head 节点,以及把处理过的每个节点的左指针设置成 NULL,新的树里面每个节点都只有右指针。

    迭代Java实现

     1 class Solution {
     2     public TreeNode increasingBST(TreeNode root) {
     3         // corner case
     4         if (root == null) {
     5             return null;
     6         }
     7         
     8         // normal case
     9         Deque<TreeNode> stack = new ArrayDeque<>();
    10         TreeNode cur = root;
    11         TreeNode pre = null;
    12         TreeNode head = null;
    13         while (!stack.isEmpty() || cur != null) {
    14             while (cur != null) {
    15                 stack.push(cur);
    16                 cur = cur.left;
    17             }
    18             cur = stack.pop();
    19             if (head == null) {
    20                 head = cur;
    21             }
    22             cur.left = null;
    23             if (pre != null) {
    24                 pre.right = cur;
    25             }
    26             pre = cur;
    27             cur = cur.right;
    28         }
    29         return head;
    30     }
    31 }

    递归Java实现

     1 class Solution {
     2     TreeNode pre = null;
     3     TreeNode head = null;
     4 
     5     public TreeNode increasingBST(TreeNode root) {
     6         if (root == null) {
     7             return null;
     8         }
     9         increasingBST(root.left);
    10         if (head == null) {
    11             head = root;
    12         }
    13         if (pre != null) {
    14             root.left = null;
    15             pre.right = root;
    16         }
    17         pre = root;
    18         increasingBST(root.right);
    19         return head;
    20     }
    21 }

    相关题目

    114. Flatten Binary Tree to Linked List

    430. Flatten a Multilevel Doubly Linked List

    897. Increasing Order Search Tree

    LeetCode 题目总结

  • 相关阅读:
    js中盒子模型常用的属性你还记得几个?
    编写一个关于浏览器盒子模型的方法
    Javascript中关于作用域和闭包和域解释的面试题
    时间格式转换
    HDU Subset sequence
    bugku never give up
    HDU 2136 Largest prime factor
    HDU 2099 整除的尾数
    杭电acm 2070
    ACM Elevator
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14083763.html
Copyright © 2011-2022 走看看