Leetcode: maximum_depth_of_binary_tree solution,

Source: Internet
Author: User

Leetcode: maximum_depth_of_binary_tree solution,

I. Question

Returns the maximum depth of a binary tree. The maximum depth is the longest path from the root node to the leaf node.

Ii. Analysis

(Here we can find that recursive statements are used for several successive questions, probably because of the simple work we have made .)

Find the deepest path, and Add 1 to each passing node. When an empty node is encountered, 0 is returned.

I have looked at some practices on the Internet in addition to the trouble but very good methods, such as using stacks and queues.


/*** Definition for binary tree * struct TreeNode {* int val; * TreeNode * left; * TreeNode * right; * TreeNode (int x): val (x ), left (NULL), right (NULL) {}*}; */class Solution {public: int maxDepth (TreeNode * root) {if (root = NULL) return 0; return max (maxDepth (root-> left), maxDepth (root-> right) + 1 ;}}; queue/*** Definition for binary tree * struct TreeNode {* int val; * TreeNode * left; * TreeNode * r Ight; * TreeNode (int x): val (x), left (NULL), right (NULL) {}*}; */class Solution {public: // maximum depth of a binary tree (layered traversal, traversing a layer height plus 1) int maxDepth (TreeNode * root) {int height = 0, rowCount = 1; if (root = NULL) {return 0;} // create the queue <treenode *> queue; // Add the root node queue. push (root); // traverse the while (! Queue. empty () {// queue Header element TreeNode * node = queue. front (); // output queue. pop (); // The number of elements in one layer minus 1, and the traversal height of one layer plus 1 rowCount --; if (node-> left) {queue. push (node-> left);} if (node-> right) {queue. push (node-> right);} // if (rowCount = 0) after a layer traversal {// The height is increased by 1 height ++; // number of elements in the next layer rowCount = queue. size () ;}} return height ;};</treenode *> stack/*** Definition for binary tree * struct TreeNode {* int val; * TreeNode * left; * TreeN Ode * right; * TreeNode (int x): val (x), left (NULL), right (NULL) {}*}; */class Solution {public: int maxDepth (TreeNode * root) {if (root = NULL) return 0; stack <treenode *> S; int maxDepth = 0; TreeNode * prev = NULL; S. push (root); while (! S. empty () {TreeNode * curr = S. top (); if (prev = NULL | prev-> left = curr | prev-> right = curr) {if (curr-> left) S. push (curr-> left); else if (curr-> right) S. push (curr-> right);} else if (curr-> left = prev) {if (curr-> right) S. push (curr-> right);} else {S. pop ();} prev = curr; if (S. size ()> maxDepth) maxDepth = S. size () ;}return maxDepth ;};</treenode *> DFS/*** Definition for binary t Ree * struct TreeNode {* int val; * TreeNode * left; * TreeNode * right; * TreeNode (int x): val (x), left (NULL), right (NULL) {} *}; */class Solution {public: int maxDepth (TreeNode * root) {if (root = NULL) return 0; int res = INT_MIN; dfs (root, 1, res); return res;} void dfs (TreeNode * root, int depth, int & res) {if (root-> left = NULL & root-> right = NULL & res <depth) {res = depth; return;} I F (root-> left) dfs (root-> left, depth + 1, res); if (root-> right) dfs (root-> right, depth + 1, res) ;}}; layered traversal/*** Definition for binary tree * struct TreeNode {* int val; * TreeNode * left; * TreeNode * right; * TreeNode (int x): val (x), left (NULL), right (NULL) {}*}; */class Solution {public: int maxDepth (TreeNode * root) {// number of layers in the hierarchy traversal tree. NULL indicates the split flag of each layer node. if (root = NULL) return 0; int res = 0; queue <TreeNode *> Q; Q. push (root); Q. push (NULL); while (Q. empty () = false) {TreeNode * p = Q. front (); Q. pop (); if (p! = NULL) {if (p-> left) Q. push (p-> left); if (p-> right) Q. push (p-> right);} else {res ++; if (Q. empty () = false) Q. push (NULL) ;}} return res ;}};



What is leetcode?

There are many programming and interview questions, which can be compiled and run online. It is difficult. If you can do it all by yourself, it is very helpful for large companies. I just did the questions there.

Can I use the C language for the questions on the leetcode website? Only C ++, java, and python

C ++ is compatible with C language.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.