Todou

Stay Learning

Personal Blog


Traverse A tree

The Binary Tree traverse problem:

  • pre-order
  • in-order
  • post-order

Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes’ values.

Example:

Input: [1,null,2,3]

    1
     \
     2
    /
   3

Output:[1,2,3]

My simple Python3 solution:

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

class Solution:
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root is None:
            return [] 
        return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)
Newer Post

Some useful Command

apt list –installedList the package that you have installed dpkg -iInstall the pkg that you want bash-aliasThe bash alias can save your time and life. …

bash 继续阅读
Older Post

Good source for learning Hexo

5分钟 搭建个人博客 Hexo and Markdown …

继续阅读