LeetCode 114. 二叉树展开为链表

2021-08-26  本文已影响0人  陈陈chen

1、题目

image.png

2、分析

可以使用变形的后序递归遍历算法(先遍历右孩子,再遍历左孩子),其他的递归遍历会导致右孩子节点指向不对。具体可以参考大神的题解:
https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--26/

3、代码

用变形的后序遍历最简单,业务代码就是,将当前节点的右孩子,赋值为上一个遍历的节点:
步骤:


image.png

代码:

/**
 * 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 {
    TreeNode pre = null;
    public void flatten(TreeNode root) {
        if (root == null) return;
        flatten(root.right);
        flatten(root.left);
        root.right = pre;
        root.left = null;
        pre = root;
    }
}

也可以用前序遍历来实现。正常的,没加任何业务代码的前序遍历代码:

public static void preOrderStack(TreeNode root) {
    if (root == null){
        return;
    }
    Stack<TreeNode> s = new Stack<TreeNode>();
    s.push(root);
    while (!s.isEmpty()) {
        TreeNode temp = s.pop();
        System.out.println(temp.val);
        if (temp.right != null){
            s.push(temp.right);
        }
        if (temp.left != null){
            s.push(temp.left);
        }
    }
}

这到题目中,我们要添加的业务代码,处理步骤为:
1、遍历到某一个节点时,将父节点的右孩子改为当前节点(因此需要将上一次遍历的节点地址,用一个变量pre保存下来)
2、将当父节点的左孩子改为null
3、变量pre = 当前节点

代码:

/**
 * 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 {
    public void flatten(TreeNode root) {
        if (root == null) return;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        TreeNode pre = null;
        while(!stack.empty()){
            TreeNode tmp = stack.pop();
            if(pre != null){
                pre.right = tmp;
                pre.left = null;
            }

            if(tmp.right != null){
                stack.push(tmp.right);
            }
            if(tmp.left != null){
                stack.push(tmp.left);
            }
            pre = tmp;
        }
    }
}

最后,还可以参考labuladong的算法:https://mp.weixin.qq.com/s/izZ5uiWzTagagJec6Y7RvQ

上一篇下一篇

猜你喜欢

热点阅读