题目:Populating Next Right Pointers in Each Node
<span style="font-size:18px;">/* * LeetCode Populating Next Right Pointers in Each Node * 题目:为树的每一个节点添加一个next指针。指向树状结构排列时自己的右边节点,假设右边没有节点则置为null * * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */ package javaTrain; public class Train10 { public void connect(TreeLinkNode root) { if(root == null) return; root.next = null; connectHelp(root.left,root.right); } private void connectHelp(TreeLinkNode left,TreeLinkNode right){ if(left == null && right == null) return; if(right == null) left.next = null; else left.next = right; connectHelp(left.left,left.right); connectHelp(left.right,right.left); connectHelp(right.left,right.right); } } </span>
时间: 2024-10-13 16:00:14