Leetcode the original
Their thinking
If the k value of the child node is odd, it will have the same value as the parent node, and conversely, it will have the opposite value
With these two rules in mind, let’s think of a process that starts at the bottom node and goes up to k = 1, where the current node is the far left of the row and all nodes with k = 1 have a value of 0
So we go back up from the target node, and as we go back we record the flag as its relationship to the parent node
Finally, when the node k = 1 is backtracked, because the value of this node must be 0, the value of the target node can be deduced according to the state of flag at this time
If flag = true, the target node has the same value as the object and returns 0; otherwise, 1 is returned
Var kthGrammar = function(n, k) {// flag let flag = true; / / when k! While (k>1){// If (k>1){ (k%2)) flag = ! flag; K = math.ceil (k/2); } if(flag) return 0; return 1; };Copy the code