116. 填充每个节点的下一个右侧节点指针

 

 

 

 

 

 1 class Solution {
 2     public Node connect(Node root) {
 3         dfs(root);
 4         return root;
 5     }
 6 
 7     void dfs(Node root){
 8         if(root==null)  return;
 9         Node left = root.left;
10         Node right = root.right;
11         while(left!=null){
12             left.next = right;
13             left = left.right;
14             right = right.left;
15         }
16         dfs(root.left);
17         dfs(root.right);
18     }
19 }

 

posted @ 2022-01-19 17:44  DXYE  阅读(20)  评论(0编辑  收藏  举报