码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-05-26 23:43:29      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

原题地址: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

标签:style   c   class   blog   code   java   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!