Hard-题目6:117. Populating Next Right Pointers in Each Node II

原创 2016年05月31日 23:24:28

题目原文:
Follow up for problem “Populating Next Right Pointers in Each Node”.

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

You may only use constant extra space.
For example,
Given the following binary tree,

         1
       /  \
      2    3
     / \    \
    4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

题目大意:
给一个普通二叉树添加next节点。
题目分析:
Middle-题目17类似,区别是这里面的二叉树是普通二叉树,上一题则是完全二叉树。所以左孩子的next不一定是右孩子,右孩子的next也不一定是父节点next的左孩子,而是从父节点开始,一直向右找到一个有孩子的同层节点,然后指向其孩子(如果有左孩子就是左孩子,没有就是右孩子)。
所以每排完一层之后向下深搜的时候,要先搜右子树,再搜左子树,故本题要用逆前序遍历!!!,如果是常规的前序遍历,向左子树深搜的时候由于右子树没处理,会有一些next没连上。
源码:(language:java)

public class Solution {
    public void connect(TreeLinkNode root) {
        if(root!=null) 
            dfs(root,root.left,root.right);

    }
    private void dfs(TreeLinkNode parent, TreeLinkNode leftchild, TreeLinkNode rightchild) { // asserted that parent isn't null
        TreeLinkNode p = parent;
        while (p.next!=null && (p==parent || (p.left==null&&p.right==null)))
            p=p.next;
        if(leftchild!=null) {
            if(rightchild!=null)
                leftchild.next = rightchild;
            else {
                if(p!=parent)
                    leftchild.next = p.left!=null?p.left:p.right;
            }

        }
        if(rightchild!=null) {
            if(p!=parent)
                rightchild.next = p.left!=null?p.left:p.right;

        }
        if(rightchild!=null)
            dfs(rightchild, rightchild.left, rightchild.right);
        if(leftchild!=null)
            dfs(leftchild, leftchild.left, leftchild.right);

    }       
}

成绩:
2ms,beats 32.02%,众数2ms,39.09%
Cmershen的碎碎念:
Follow up有说到,要常数的空间复杂度,不知道递归是不是不合要求的?如果不合要求,那么可以通过改造前序遍历来实现,貌似会很麻烦……

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode:Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node II Total Accepted: 63428 Total Submissions: 192670 ...

[leetcode]117. Populating Next Right Pointers in Each Node II

注意一个事实:到根节点的左子节点的next要么指向根节点的右子节点(右子节点不为空),要么就指向根节点的next(或者next的next)节点p,直到该p节点具有下一代的节点。也就是说,将每一层的ne...

Leetcode 117. Populating Next Right Pointers in Each Node II 链接邻居2 解题报告

1 解题思想这道题和116基本一样: Leetcode 116. Populating Next Right Pointers in Each Node 链接邻居 解题报告 区别在于这个是任意二叉树...
  • MebiuW
  • MebiuW
  • 2016年09月30日 22:52
  • 1149

[LeetCode]problem 117. Populating Next Right Pointers in Each Node II

TAG层序遍历 - 常量空间,通过next指针二叉树link方法有了Populating next right pointers in each node I的铺垫,这道题就显得没有任何思维上的限制了...

Populating Next Right Pointers in Each Node II - LeetCode 117

题目描述: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree co...
  • bu_min
  • bu_min
  • 2015年05月23日 20:54
  • 203

LeetCode 117 Populating Next Right Pointers in Each Node II

题目 Given a binary tree struct TreeLinkNode { TreeLinkNode...
  • seawade
  • seawade
  • 2014年03月05日 12:52
  • 536

[leetcode] 117. Populating Next Right Pointers in Each Node II 解题报告

题目链接:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for p...

leetcode_question_117 Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could b...
  • doc_sgl
  • doc_sgl
  • 2013年09月22日 22:26
  • 1207

[LeetCode]117.Populating Next Right Pointers in Each Node II

【题目】 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree ...

LeetCode 117: Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node IIFollow up for problem “Populating Next Right Pointers ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目6:117. Populating Next Right Pointers in Each Node II
举报原因:
原因补充:

(最多只允许输入30个字)