Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to return post-order traversal of python binary tree

Shulou Source: shulou.com Published: 2022-06-01 14:11:50 10月19日 Update

This article shows you how to return to the subsequent traversal of the python binary tree, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Brief introduction of the problem.

Given a binary tree, return its post-order traversal.

2, example

Input: [1memnullre2] 1 / 2 / 3 output: [3meme2pence 1]

3, the train of thought of solving the problem

Solve the problem by recursion

4, problem solving procedure

Import java.util.ArrayList;import java.util.List

Public class PostorderTraversalTest2 {public static void main (String [] args) {TreeNode T1 = new TreeNode (1); TreeNode T2 = new TreeNode (2); TreeNode T3 = new TreeNode (3); t1.right = T2; t2.left = T3; List list = postorderTraversal (T1); System.out.println ("list =" + list)

}

Private static List list = new ArrayList ()

Public static List postorderTraversal (TreeNode root) {if (root = = null) {return list;} dfs (root); return list;}

Private static void dfs (TreeNode root) {if (root.left! = null) {dfs (root.left);} if (root.right! = null) {dfs (root.right);} list.add (root.val);}}

5. Picture version of the problem solving program.

The above is how to return the post-order traversal of the python binary tree. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Problem solutions content skills knowledge procedures concise concise pictures that is ideas articles methods more examples articles industry information channels questions channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Docker Shulou Tech Info MariaDB