首页 > 编程语言 > 详细

[leetcode]Populating Next Right Pointers in Each Node @ Python

时间:2014-05-26 23:41:42      阅读:478      评论:0      收藏:0      [点我收藏+]

原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/

题意:

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

解题思路:看到二叉树我们就想到需要使用递归的思路了。直接贴代码吧,思路不难。

代码:

bubuko.com,布布扣
# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#         self.next = None

class Solution:
    # @param root, a tree node
    # @return nothing
    def connect(self, root):
        if root and root.left:
            root.left.next = root.right
            if root.next:
                root.right.next = root.next.left
            else:
                root.right.next = None
            self.connect(root.left)
            self.connect(root.right)
bubuko.com,布布扣

 

 

[leetcode]Populating Next Right Pointers in Each Node @ Python,布布扣,bubuko.com

[leetcode]Populating Next Right Pointers in Each Node @ Python

原文:http://www.cnblogs.com/zuoyuan/p/3745170.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!