题目

Given a binary tree

    struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,
Given the following perfect binary tree,

         1
/ \
2 3
/ \ / \
4 5 6 7

After calling your function, the tree should look like:

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

题解

这道题解法还是挺直白的,如果当前节点有左孩子,那么左孩子的next就指向右孩子。如果当前节点有右孩子,那么判断,如果当前节点的next是null,
说明当前节点已经到了最右边,那么右孩子也是最右边的,所以右孩子指向null。如果当前节点的next不是null,那么当前节点的右孩子的next就需要
指向当前节点next的左孩子。
递归求解就好。 代码如下:
 1     public void connect(TreeLinkNode root) {
 2         if(root==null)
 3             return;
 4         if(root.left!=null)
 5             root.left.next=root.right;
 6         if(root.right!=null){
 7             if(root.next!=null)
 8                 root.right.next = root.next.left;
 9             else
                 root.right.next = null;
         }
         connect(root.left);
         connect(root.right);
     }
												

Populating Next Right Pointers in Each Node leetcode java的更多相关文章

  1. leetcode 117 Populating Next Right Pointers in Each Node II ----- java

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  2. Populating Next Right Pointers in Each Node [LeetCode]

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  3. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  4. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  5. [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  6. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  7. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

  8. 【LeetCode OJ】Populating Next Right Pointers in Each Node II

    Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...

  9. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

随机推荐

  1. 【BZOJ 4818】 4818: [Sdoi2017]序列计数 (矩阵乘法、容斥计数)

    4818: [Sdoi2017]序列计数 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 560  Solved: 359 Description Al ...

  2. BZOJ.4032.[HEOI2015]最短不公共子串(DP 后缀自动机)

    题目链接 1.求A的最短子串,它不是B的子串. 子串是连续的,对B建SAM,枚举起点,在SAM上找到第一个无法匹配点即可.O(n)用SAM能做吗..开始想错了. 2.求A的最短子串,它不是B的子序列. ...

  3. 关于dubbo服务的xml配置文件报错的问题

    在配置dubbo服务的过程中,经常会遇到虽然程序能够跑起来,但是配置文件一堆红叉,虽然不影响功能,但是确实很让人恶心. 报错信息如下: Multiple annotations found at th ...

  4. BZOJ2832 : 宅男小C

    首先将所有显然不在最优解中的外卖都删去,那么剩下的外卖价格越低,保质期也最短. 考虑三分订外卖的次数,然后贪心求解,每次尽量平均的时候可以做到最优化. 三分的时候,以存活天数为第一关键字,剩余钱数为第 ...

  5. 2018-2019-20172329 《Java软件结构与数据结构》第五周学习总结

    2018-2019-20172329 <Java软件结构与数据结构>第五周学习总结 教材学习内容总结 <Java软件结构与数据结构>第九章-排序与查找 一.查找 1.查找概念简 ...

  6. 重写对象ToString方法

    重写对象ToString方法,引入Newtonsoft.Json public override string ToString() { JsonSerializerSettings settings ...

  7. 群晖NAS使用Docker安装迅雷离线下载出现the active key is not valid.

    出现这种情况多半是挂了,也有可能是不稳定的网络,重装Docker镜像可能会解决,只有不断试,没什么好的解决方法.

  8. 使用Apache commons-codec Base64实现加解密(转)

    commons-codec是Apache下面的一个加解密开发包 官方地址为:http://commons.apache.org/codec/ 官方下载地址:http://commons.apache. ...

  9. STM32F4 Timer Internal Trigger Connection

    The Timers can be cascaded to make more complex timing relationships, or longer periods. Internally ...

  10. SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic

    SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic The SW-DP protocol is described ...