Copyright Notice:
This article is published by Faye_zuo in http://www.cnblogs.com/zuofeiyi/, this article can be reproduced or partially used, but please specify the source.
I learned about arrays and linked lists last week, and a little bit more on the basics, this week we explored the depth-first search. This noun sounds a bit scary to me for the liberal arts students. So I get to know this concept gradually through a difficulty of leetcode on the topic of medium. This topic is validate Binary Search Tree (question number 98). Below I will use this problem as a primer to organize what I learned last week.
One, a tree.
This topic is to let us judge whether this tree is a valid binary search tree. But I don't even know what a tree is. Later through the study on YouTube video, he has a gradual understanding. The concept of a tree is easy to understand, and now it's mainly about the storage structure (I don't know what the storage structure is for, actually).
The tree's storage structure is as follows:
(1) The parent representation takes a contiguous space to store the nodes of the tree, and in each node, an indicator is attached to the position of the parent node to the linked list. In this way, each node knows the location of his or her parents in addition to who knows who he is. (2) Child representation: A multi-linked list (3) Parent-child notation: The combination of the two above
Two or two fork trees (binary tree)
With the understanding of the tree, the basic concept of the binary tree is easier to understand.
1. Concept: A tree with a maximum of two nodes per node, usually a subtree called a Saozi right subtree. 2. Establishment of a two-fork tree (very important) public static void main (string[] args) {/* Making a tree like:0/\ 1 2/3 */TreeNode root = new TreeNode (0); Root.left = new TreeNode (1); Root.right = new TreeNode (2); Root.left.left = new TreeNode (3); 3. Traversal of the two fork tree private static class TreeNode {Public TreeNode (int x) {///Red flag part is not a member function, is a constructor, mainly used for initialization. Therefore, there is no need to have a return value type. this.x = x; } public int x; public TreeNode left;//left and right are variables public TreeNode RIGHT;&N Bsp } /* Pre-order, first-order traversal */ private static void Traversetreepreorder (TreeNode node) { if (node ! = null) { SYSTEM.OUT.PRINTLN (node.x); Traversetreepreorder (node.left); & nbsp Traversetreepreorder (node.right); } } /* in-order, Middle sequence traversal */ private static Voi D Traversetreeinorder (TreeNode node) { if (node! = null) { Traversetreeinorder (node.left) ; SYSTEM.OUT.PRINTLN (node.x); Traversetreeinorder (node.right); }& nbsp } /* Post-order, post-traverse */ private static void Traversetreepostorder (TreeNode node) { if (nod E! = null) { Traversetreepostorder (node.left); Traversetreepostorder (node.right ); &NBsp System.out.println (node.x); } } Three, and then I can start to look at this leetcode topic. Validate Binary Search Tree (98)Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: (1) The left subtree of a node contains only nodes with keys less than the node ' s key. (2 The right subtree of a node contains only nodes with keys greater than the node's key. (3) Both the left and right subtrees must also be binary search trees. Do not do at all, so only to see the answer, found that the problem is mainly to use recursion, the idea is as follows: 1. All the values on the left side must be less than root, and all values on the right side must be greater than root. 2. We want to see if each node is satisfied with BST. 3. We want to put this upper and lower limit into recursion. Complete solution of the program: public class Binarysearchtree {
public static void Main (string[] args) {
TreeNode root=new TreeNode (6);
Root.left=new TreeNode (1);
Root.right=new TreeNode (7);
Root.left.left=new TreeNode (4);
Solution A=new solution (); System.out.println (A.isvalidbst (root));//Why do I need to change the following method to static when new objects are added? The reason is to look at the following method is not//is to access the unique data in the object. If you have access to, add static.
}
PublicStaticClass treenode{
int Val;
TreeNode left;
TreeNode right;
TreeNode (int x) {
Val=x;
}
}
PublicStaticClass solution{
public Boolean isvalidbst (TreeNode root) {
Return Isvalidbst (root,double.negative_infinity,double.positive_infinity);
}
public boolean Isvalidbst (TreeNode p,double min, double max) {
if (p==null)
return true;
if (p.val<=min| | P.val>=max)
return false;
Return Isvalidbst (P.left,min,p.val) &&isvalidbst (P.right,p.val,max);
}
} }in the above procedures, I found that I have a lot of knowledge points need to learn, and then add. Tidy up as follows: 1. Some fixed need to remember classdouble.negative_infinity indicates that a negative number infinitesimal double.positive_infinity represents a positive infinity of 2.an algorithm that needs to be rote (it's really good to write)public Boolean isvalidbst (TreeNode root) {
Return Isvalidbst (root,double.negative_infinity,double.positive_infinity);
}
public boolean Isvalidbst (TreeNode p,double min, double max) {
if (p==null)
return true;
if (p.val<=min| | P.val>=max)
return false;
Return Isvalidbst (P.left,min,p.val) &&isvalidbst (P.right,p.val,max); } Explanation of static knowledge points in 3.java (1) member variables are also called instance variables, but static variables are also called class variables, which are variables that the class can access directly. (2) The difference between a member variable and a static variable: Life cycle: member variables exist as objects are created and released as objects are recycled. Static variables exist with the existence of the class and disappear as the class disappears. Invocation mode: Member variables can only be called by an object. A static object can be called by a class name, or it can be called by an object. The data store location is different: the member variable is stored in the object of the heap memory, and the static variable is stored in the method area. (3) Static in the use of the process should pay attention to the details: A. Static methods can only access static members.Note A common error: Non-static cannot be called from a static method. The reason is that static variables are loaded first, and there are no objects in the non-static variables when loading. B. The this or Super keyword cannot be used in a static method. C. The main function must be static. (4) When to use static variables: When the value of a member variable in an object is the same, the member can be statically decorated. Static functions:Pay attention! Does the function have access to the unique data in the object (unique data: The value of the object). 4.Recursive(1). The most basic idea of recursion: Call Yourself (2). Recursion must have an exit, that is, tell others when you can not call. Otherwise, this function will enter a dead loop until the stack overflows. To be blunt is an if else (3). Characteristics of recursion: time consuming, space consuming (one layer of call, one layer of return) 5. Supplementary object-oriented basics (1) What is a method: The method is the overload of the Method (methods) (2): In fact, the same method name, through the different parameters, you can implement different functions of the function. public void-Tell (int i,int j) {}public void-tell (string k) {} What you learned this week is roughly collated and shared here, and next week you're ready to practice a few more leetcode questions about the tree. I hope we can have a deeper understanding of this data structure of the tree! Come on!
First Depth First search--java study notes (ii)