Populating Next Right Pointers in Each Node II
2015-08-06 15:05
555 查看
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,
After calling your function, the tree should look like:
思路和上一个相似,只是加入了对左右是否为空的判断。
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
思路和上一个相似,只是加入了对左右是否为空的判断。
public void connect(TreeLinkNode root) { if(root==null) return; TreeLinkNode parent=root; TreeLinkNode next,pre; while(parent!=null){ next=null; pre=null; while(parent!=null){ if(next==null) next=parent.left!=null?parent.left:parent.right; if(parent.left!=null){ if(pre==null) pre=parent.left; else{ pre.next=parent.left; pre=pre.next; } } if(parent.right!=null){ if(pre==null) pre=parent.right; else{ pre.next=parent.right; pre=pre.next; } } parent=parent.next; } parent=next; } }
相关文章推荐
- UbuntuServer安装Node.js
- sudo 运行 npm或node时,出现找不到命令
- Populating Next Right Pointers in Each Node
- ENode简介与各种教学视频资源汇总(要进群这篇文章必看)
- 30. Element parentNode 属性
- 28. Element nodeValue 属性
- 27. Element nodeType 属性
- 26. Element nodeName 属性
- Node.js 文件系统fs模块
- Delete Node in a Linked List
- Node.js 异步式 I/O 与事件驱动实战
- [LeetCode]Swap Nodes in Pairs
- Zookeeper的web管理系统
- NodeJS遍历文件生产文件列表
- NodeJs简介
- 24 Swap Nodes in Pairs
- node.js async 几个函数
- 13. Element childNodes 属性
- nodejs tracer 模块实现日志跟踪
- nodejs前端跨域访问