Jan 29 - Flatten Binary Tree To Linked List; DFS;

Using DFS to traverse the tree.

4 cases for each node in the tree:

case 1: it‘s a leaf node, set a pointer pointing to the node. We‘re reaching an end of one dfs search.

case 2: has left child, and no right child, set the right child to be left one, and set left to be null, go into new right child.

case 3: no left child, right child exists. Just go directly into the right child.

case 4: both left and right child exist. First, using dfs to traverse the left subtree, retrieve the former leaf pointer, set its right child to be current node‘s right child. And dfs to traverse the right child one.

Code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    TreeNode deadend = null;
    public void flatten(TreeNode root) {
        if(root == null) return;
        rotate(root);

    }

    public void rotate(TreeNode node){
        if(node.left == null && node.right == null) {
            deadend = node;
            return;
        }
        if(node.left != null && node.right == null){
            node.right = node.left;
            node.left = null;
            rotate(node.right);
        }
        else if(node.left == null && node.right != null) rotate(node.right);
        else{
            TreeNode temp = node.right;
            node.right = node.left;
            node.left = null;
            rotate(node.right);
            //System.out.println(deadend.val);
            deadend.right = temp;
            rotate(deadend.right);
        }
    }

    /*
    public TreeNode rotate(TreeNode node){
        TreeNode deadend = node;
        TreeNode temp = node.right;

        if(node.left != null){
            node.right = node.left;
            deadend = rotate(node.left);
            node.left = null;

        }
        if(node.right != null){
            deadend.right = temp;
            deadend = rotate(node.right);
        }
        return deadend;
    }
    */

}
时间: 2024-08-27 16:05:04

Jan 29 - Flatten Binary Tree To Linked List; DFS;的相关文章

Flatten Binary Tree to Linked List (DFS)

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 代码: class Solution{ public: void flatten(TreeNode *root) { if(root==NULL) return; TreeNode* p=root->left;

[Leetcode][Tree][Flatten Binary Tree to Linked List ]

按照前序遍历的顺序把树用right连起来. 本来想了半天,一点思路都没有,总觉得Inplace的解法一般都非常巧妙. 后来我突发灵感,决定用一个变量记录当前访问到哪个点,真是太机智了~~ 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), r

Leetcode:Flatten Binary Tree to Linked List 解题报告

Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 SOLUTION 1:使用递归解决,根据left是否为空,先连接left tree, 然后再连接右子树.使用一个tail 来记录链的结尾.在递

leetcode dfs Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 click to

【LeetCode】Flatten Binary Tree to Linked List

题目:Flatten Binary Tree to Linked List <span style="font-size:18px;">/**LeetCode Flatten Binary Tree to Linked List * 题意:给定一个二叉树,将其转变为一个相当于单链表的结构,观察可知该结构即:每一个节点左儿子为空,右儿子指向自己先序遍历时的下一个节点 * 思路:有观察可得,应对其进行先序遍历,得到正确的序列连接起来 * Definition for binar

Flatten Binary Tree to Linked List leetcode java

题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题解:如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空.注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子.整个程序需要维护一个全局变量,保

leetcode 之 Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 思路:对二叉树进行先序遍历,把左子树为空的节点的左子树指向它的后序节点,然后把所有的右子树指向左子树 struct TreeNode { i

leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r

[leetcode]Flatten Binary Tree to Linked List @ Python

原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6Hints: If you notice carefully