Take you to brush the binary tree (Phase 2)

Previous Article taught you how to brush binary tree (first post) even brush the three binary topic, many readers address him the ball. In fact, the algorithm related to the binary tree is really not difficult. This article will do three more articles to show you how to do the tree algorithm.

Let's review first, we have talked about the algorithm for writing trees, the key ideas are as follows:

Refine the requirements of the topic, figure out what the root node should do, and then leave the rest to the pre/middle/post-order traversal framework . We must not jump into the recursive details, your head can be suppressed A few stacks.

Maybe you still don't understand this sentence, let's look at an example below.

Construct the largest binary tree

Let’s start with a simple answer. This is the 654th question. The title is as follows:

deb795b7a1e9656ec27d580d00682857.png

The function signature is as follows:

TreeNode constructMaximumBinaryTree(int[] nums);

According to what we just said, first clarify what the root node does? For the problem of constructing a binary tree, what the root node has to do is to find a way to construct itself .

We certainly have to traverse the array to find the maximum value , the root node to do it, then , and an array of arrays on the left to the right of recursive calls, as the left and right subtrees.maxValrootmaxValroot

According to the example given in the title, the input array is , for the root node of the entire tree, it is actually doing this:[3,2,1,6,0,5]

TreeNode constructMaximumBinaryTree([3,2,1,6,0,5]) {// Find the maximum value in the array TreeNode root = new TreeNode(6); // Recursively call to construct the left and right subtree root.left = constructMaximumBinaryTree([ 3,2,1]); root.right = constructMaximumBinaryTree([0,5]); return root;}

In more detail, it is the following pseudocode:

TreeNode constructMaximumBinaryTree(int[] nums) {if (nums is empty) return null; // Find the maximum value in the array int maxVal = Integer.MIN_VALUE; int index = 0; for (int i = 0; i <nums.length ; i++) {if (nums[i]> maxVal) {maxVal = nums[i]; index = i;}} TreeNode root = new TreeNode(maxVal); // Recursive call to construct left and right subtree root.left = constructMaximumBinaryTree( nums[0..index-1]); root.right = constructMaximumBinaryTree(nums[index+1..nums.length-1]); return root;}

Understand? For each root, just need to find the current maximum and the corresponding index, then left about an array of recursive calls to sub-tree structure .nums

Clear thinking, we can re-write a helper function to control the index:buildnums

/* Main function*/TreeNode constructMaximumBinaryTree(int[] nums) {return build(nums, 0, nums.length-1);}/* Construct nums[lo..hi] into a qualified tree and return the root node */TreeNode build(int[] nums, int lo, int hi) {// base case if (lo> hi) {return null;} // find the maximum value in the array and the corresponding index int index = -1, maxVal = Integer.MIN_VALUE; for (int i = lo; i <= hi; i++) {if (maxVal <nums[i]) {index = i; maxVal = nums[i];}} TreeNode root = new TreeNode( maxVal); // Recursive call to construct left and right subtree root.left = build(nums, lo, index-1); root.right = build(nums, index + 1, hi); return root;)

At this point, this problem is finished. It is quite simple, right? Let's look at two more difficult common algorithm problems: let you restore the binary tree with the pre-order/in-order traversal results, and use the post-order/in-order traversal results to restore Binary tree.


Guess you like

Origin blog.51cto.com/15064450/2570995